<?php

/**
 * 通过msdk登录态信息获取用户基本信息
 * Date: 2019/09/03
 */

namespace Lib\Paas;

use Lib\Base\Request;
use Lib\Base\Validate;

class MsdkInfo extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取用户基本信息
     * @return array
     * [
     *      'iRet' => '0 成功，!=0 失败',
     *      'sMsg' => '系统消息',
     *      'data' =>
     *      [
     *          'nickName' => '昵称',
     *          'picture' => '图像',
     *          'gender' => '性别：男/女',
     *      ]
     * ]
     */
    public function getUserInfo($data = [])
    {
        if (empty($data)) {
            $userInfo = Request::param(['algorithm', 'version', 'timestamp', 'appid', 'sig', 'encode', 'msdkEncodeParam']);
        } else {
            $userInfo = $data;
        }

        if (!Validate::checkNaturalNum($userInfo['timestamp'])) {
            return $this->getRetData('-1000803', '时间戳错误！');
        }
        if ($userInfo['msdkEncodeParam'] == '') {
            return $this->getRetData('-1000804', '登录态相关参数错误！');
        }

        if ($userInfo['appid'] == '') {
            return $this->getRetData('-1000805', '登录态相关参数错误！');
        }

        if ($userInfo['sig'] == '') {
            return $this->getRetData('-1000806', '登录态相关参数错误！');
        }

        if ($userInfo['encode'] == '') {
            return $this->getRetData('-1000807', '登录态相关参数错误！');
        }

        if ($userInfo['algorithm'] == '') {
            return $this->getRetData('-1000808', '登录态相关参数错误！');
        }

        if ($userInfo['version'] == '') {
            return $this->getRetData('-1000809', '登录态相关参数错误！');
        }

        $url = $this->getPaasRequestUrl('user.msdkinfo', $userInfo, false);
        $res = $this->http->get($url);

        recordLog('msdkinfo查询paas接口返回值：' . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'user.msdkinfo');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];
        if ($res['ret'] != '0') {
            return $this->getRetData('-1000810', '系统繁忙，请稍后再试！');
        }
        $msdkData = $res['data'];
        $retData = [];

        //QQ平台
        if (Validate::checkNaturalNum($userInfo['appid'])) {
            $retData['picture'] = $msdkData['picture100'];
            $retData['gender'] = $msdkData['gender'];
            $retData['nickName'] = $msdkData['nickName'];
        } else {
            $retData['picture'] = $msdkData['lists'][0]['picture'] . '/132';
            $retData['gender'] = $msdkData['lists'][0]['sex'] == '1' ? '男' : '女';
            $retData['nickName'] = $msdkData['lists'][0]['nickName'];
        }
        return $this->getRetData('0', 'success', ['data' => $retData]);
    }
}
